package any.incubator;

import java.util.Collections;
import java.util.Iterator;

public class Options {

	public static <T> Option<T> some(T t) {
		return new Some<T>(t);
	}

	public static <T> Option<T> none() {
		return new None<T>();
	}

	public static class None<T> implements Option<T> {

		public boolean hasNone() {
			return true;
		}

		public boolean hasSome() {
			return false;
		}

		public T orSome(T value) {
			return value;
		}

		public int size() {
			return 0;
		}

		public T some() throws IllegalStateException {
			throw new IllegalStateException();
		}

		public Iterator<T> iterator() {
			return Collections.<T>emptyList().iterator();
		}

	}

	public static class Some<T> implements Option<T> {

		private final T value;

		public Some(T value) {
			this.value = value;
		}

		public boolean hasNone() {
			return false;
		}

		public boolean hasSome() {
			return true;
		}

		public T orSome(T value) throws IllegalStateException {
			return this.value;
		}

		public int size() {
			return 1;
		}

		public T some() throws IllegalStateException {
			return value;
		}

		public Iterator<T> iterator() {
			return Collections.singletonList(value).iterator();
		}

	}

}
